<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>简书</title>
  </head>
  <body>
    <!-- 这个好像有点麻烦了，就不再多进入看了，以后有机会再看吧 -->
    <script>
      if ("serviceWorker" in navigator) {
        window.addEventListener("load", function () {
          const sw = window.navigator.serviceWorker;
          const killSW = window.killSW || false;
          if (!sw) {
            return;
          }
          if (!!killSW) {
            sw.getRegistration("/serviceWorker").then((registration) => {
              // 手动注销
              registration.unregister(); // 清除缓存
              window.caches &&
                caches.keys &&
                caches.keys().then(function (keys) {
                  keys.forEach(function (key) {
                    caches.delete(key);
                  });
                });
            });
          } else {
            // 表示该 sw 监听的是根域名下的请求
            sw.register("/serviceWorker.js", { scope: "/" })
              .then((registration) => {
                // 注册成功后会进入回调
                console.log("Registered events at scope: ", registration.scope);
              })
              .catch((err) => {
                console.error(err);
              });
          }
        });
      }
    </script>
  </body>
</html>
